<?php namespace simpleFlow\app\module\admin\control\publik;

use simpleFlow\request\RequestWeb as SFRequest;
use simpleFlow\app\model\user\User as User;
use simpleFlow\Session as SFSession;
use simpleFlow\view\html as SFHtml;
use simpleFlow\app\module\admin\control\users\Control as UserControl;
use simpleFlow\control\Control as SFControl;

class Control extends SFControl
{
	public function viewLogin(SFRequest $objRequest)
	{
		$obj = new SFHtml\InputText('txUsername');
		$obj->createLabel('Login');
		$this->objView->txUsername = $obj;
		
		$obj = new SFHtml\InputPassword('txPassword');
		$obj->createLabel('Password');
		$this->objView->txPassword = $obj;
		
		$obj = new SFHtml\InputSubmit('btSubmit','Login');
		$this->objView->btSubmit = $obj;
		
		unset($obj);
		return $this->renderView('viewLogin.tpl');
	}
	
	public function viewIndex(SFRequest $objRequest)
	{
		/*$ctrlUsers = new UserControl($this->objView,true);
		$this->objView->setFetchMode(true);
		
		$this->objView->listUsers = $ctrlUsers->viewIndex($objRequest);
		
		$this->objView->setFetchMode(false);
		
		
		$ctrlAdmin = new $this($this->objView,true);
		$this->objView->setFetchMode(true);
		
		$this->objView->viewLogin = $ctrlAdmin->viewLogin($objRequest);
		
		$this->objView->setFetchMode(false);*/
		
		//$objUser = new \simpleFlow\app\model\user\User();
		//$objUser = new UserControl();
		//x( $objUser->readAll(false) );
		
		return $this->renderView('index.tpl');
	}

	public function tryLogin(SFRequest $objRequest)
	{
		$objUser = new User();
		$objUser->read(array('txUsername' => $objRequest->getPost('txUsername'),'txPassword' => $objRequest->getPost('txPassword')));
		
		if($objUser->getKey())
		{
			SFSession::addParam('User', $objUser);
			SFSession::addParam('SF_USER_LOGGED',true);
			SFSession::addParam('sf_success_message', 'Loggin success! Welcome to the simpleFlow!');
			$this->redirect('admin', 'publik', 'viewIndex');
		}else{
			SFSession::addParam('sf_error_message', 'Loggin failed!');
			$this->redirect('admin', 'publik', 'viewLogin');
		}
	}
	
	public function tryLogout(SFRequest $objRequest)
	{
		SFSession::destroy();
		$this->redirect('admin', 'publik', 'viewLogin');
	}
}

?>